-- 其若响应
local qiruoxiangying = fk.CreateSkill {
    name = "mcdh_qiruoxiangying",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

qiruoxiangying:addEffect(fk.AfterCardsMove ,{
    can_trigger = function(self, event, target, player, data)
        if player:hasSkill(self) and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 then
            for _, move in ipairs(data) do
                if move.from and move.to and move.to ~= move.from and move.toArea == Card.PlayerHand then
                    for _, info in ipairs(move.moveInfo) do
                        if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
                            return true
                        end
                    end
                end
            end
        end
    end,
    on_trigger = function(self, event, target, player, data)
        local targets = {}
        local room = player.room
        for _, move in ipairs(data) do
            if move.from and move.to and (move.from ~= move.to) then
                for _, info in ipairs(move.moveInfo) do
                    if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
                        table.insertIfNeed(targets, move.from)
                    end
                end

            end
        end
        room:sortByAction(targets)
        for _, p in ipairs(targets) do
            if p.dead or not player:hasSkill(self) then
                break
            end
            self:doCost(event,p, player, nil)
        end
        return false
    end,
    on_cost = function(self, event, target, player, data)
        return player.room:askToSkillInvoke(player,{skill_name=self.name,prompt="#mcdh__qiruoxiangying-draw:" .. target.id })
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        target:drawCards(1, self.name)
        local max__flag = true
        local x, y = target:getHandcardNum(), 0
        for _, p in ipairs(room.alive_players) do
            y = p:getHandcardNum()
            if x < y then
                max__flag = false
            end
        end
        if max__flag then
            local id = nil
            if target ~= player then
                if not target:isNude() then
                    id = room:askForCardChosen(player, target, "he", self.name)
                end
            else
                if #target:getCardIds{Player.Equip} ~= 0 then
                    id = room:askForCardChosen(player, target, "e", self.name)
                end
            end
            if id ~= nil then
                local dummy = Fk:cloneCard("dilu")
                dummy:addSubcard(id)
                room:obtainCard(player.id, dummy, false, fk.ReasonPrey)
            end
        end

    end
})

Fk:loadTranslationTable {
    ["mcdh_qiruoxiangying"] = "其应若响",
    [":mcdh_qiruoxiangying"] = "每回合限一次，当一名角色的牌被另一名角色获得后，你可以令失去牌的角色摸一张牌，然后若其手牌数为全场最多，则你获得其一张牌。",
    ["mcdh__xing"] = "信",
    ["#mcdh__qiruoxiangying-draw"] = "让 %src 摸一张牌 ,然后若其手牌最多你获得其一张牌",
}

return qiruoxiangying